//获取应用实例
import {chooseLocation} from "../../../utils/location";
import areaService from "../../../service/area";

const app = getApp();

Component({

    // behaviors: ['wx://form-field-group'],
    options:{
        styleIsolation: 'apply-shared',
    },

    properties: {
        location:{
            type:[Object,null,undefined],
            observer(data){
                if (data){
                    this.setData({_location:data})
                }
            }
        },
    },
    data: { // 私有数据，可用于模版渲染
        _location:{},
    },
    methods: {
        addressChange(e){
            const {value} = e.detail
            const {_location} = this.data
            _location.address = value
            this.setData({
                _location
            })
            this.triggerEvent("change", _location)
        },
        /**
         *  调起腾讯地图选择位置信息
         */
        setLocation(e) {
            chooseLocation().then(choose => {
                areaService.geocoder(`${choose.lat},${choose.lng}`).then(location => {

                    // 删除address，这里不能覆盖address
                    delete location.address
                    const {_location} = this.data

                    // 合并数据
                    const rl = {..._location, ...location}
                    this.setData({
                        _location:rl
                    })

                    this.triggerEvent("change", rl)

                })
            })
        },
    }
})
